import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * 实现Callable新建线程
 */
public class ThreadDemo8 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //创建Callable实例
        MyCallable myCallable=new MyCallable();
        //用于接收Callable结果的对象
        FutureTask<Integer> futureTask=new FutureTask<>(myCallable);
        //创建新线程
        Thread thread =new Thread(futureTask);
        //启动线程
        thread.start();
        //接收新线程的执行结果
        int result=futureTask.get();
        System.out.println(Thread.currentThread().getName()+"--新线程返回的结果为:"+result);
    }
}
/**
 * Callable<V> 泛型里面可以是任意数据类型
 */
class MyCallable implements Callable<Integer> {

    @Override
    public Integer call() throws Exception {
        //随机数0-9
        int randomNum=new Random().nextInt(10);
        System.out.println(Thread.currentThread().getName()+"--随机数"+randomNum);
        return randomNum;
    }
}
